package com.gklyj.controller.zhaoping;

import com.gklyj.pojo.Application;
import com.gklyj.pojo.Position;
import com.gklyj.service.applicationService;
import com.gklyj.service.positionService;
import com.gklyj.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Transactional
public class ApplicationController {

    @Autowired
    applicationService applicationService;
    @Autowired
    userService userService;
    @Autowired
    positionService positionService;

    /*
    招聘单位跳转职位审批页面
     */
    @GetMapping("/toZwspIndex")
    public String toZwspIndex(){
        return "zhaoping/zp-application";
    }

    /*
    列出相关的职位申请信息，招聘单位页面
     */
    @GetMapping("/ListZpApplication")
    @ResponseBody
    public List<Application> ListZpApplication(HttpServletRequest request, @RequestParam("state") String state){
        //获取当前登录的招聘单位信息
        String username= (String) request.getSession().getAttribute("username");
        String department = userService.findDepartmentByUsername(username);
        String state1 = null;
        //判断返回出于什么状态的申请列表
        if (state.equals("0")){
            state1="审核中";
        }else if (state.equals("1")){
            state1="已通过";
        }else {
            state1="未通过";
        }
        return applicationService.listApplicationByDepartment(department,state1);
    }

    /*
    列出相关的职位申请信息，管理员页面
     */
    @GetMapping("/ListAdApplication")
    @ResponseBody
    public List<Application> ListAdApplication(@RequestParam("state") String state){
        //获取当前登录的招聘单位信息
        String department = "广东科技学院";
        String state1 = null;
        //判断返回出于什么状态的申请列表
        if (state.equals("0")){
            state1="审核中";
        }else if (state.equals("1")){
            state1="已通过";
        }else {
            state1="未通过";
        }
        return applicationService.listApplicationByDepartment(department,state1);
    }

    /*
    弹出审批页面，显示求职者申请备注信息
     */
    @GetMapping("/toZpApplicationDetail")
    public String toZpApplicationDetailPage(@RequestParam("id") String id, Model model){
        Integer a_id = Integer.valueOf(id);
        Application application = applicationService.getById(a_id);
        model.addAttribute("application",application);
        return "zhaoping/zp-application-approval";
    }

    /*
    审批申请信息并修改申请信息状态，招聘单位部分
     */
    @PostMapping("/ZpApplicationApproval")
    @ResponseBody
    public int ZpApplicationApproval(@RequestBody Application application) throws RuntimeException{
        //查询该条申请记录
        Application application1 = applicationService.getById(application.getA_id());
            //招聘单位审核通过
            if (application.getState().equals("已通过")){
                //通过更新申请信息状态
                boolean uApply = applicationService.UpdateStateById(application.getA_id(),application.getState());
                //该职位数量加一
                boolean b = positionService.AddPositionQuantity(application1.getDepartment(), application1.getP_title());
                if (uApply && b){
                    return 1;        //表示操作成功
                }else {
                    return  0;    //表示操作失败
                }
            }else {
                //不通过更新申请信息状态
                boolean uApply = applicationService.UpdateStateById(application.getA_id(),application.getState());
                if (uApply){
                    return 1;
                }
                return 0;
            }

    }

    /*
    根据条件查找‘审核中’的申请信息，招聘单位部分
     */
    @PostMapping("/ZpDifferenceApplicationSearch")
    @ResponseBody
    public List<Application> ZpDifferenceApplicationSearch(HttpServletRequest request,@RequestParam("state") String state,@RequestBody Application application){
        //获取当前登录的招聘单位信息
        String username= (String) request.getSession().getAttribute("username");
        String department = userService.findDepartmentByUsername(username);
        application.setDepartment(department);
        //若state=0，则为审核中，，=1为已通过，，=2为未通过
        if (state.equals("0")){
            application.setState("审核中");
        }else if (state.equals("1")){
            application.setState("已通过");
        }else {
            application.setState("未通过");
        }
        return applicationService.SearchZpDifferenceApplication(application);
    }

    /*
    根据条件查找‘审核中’的申请信息，管理员部分
     */
    @PostMapping("/AdDifferenceApplicationSearch")
    @ResponseBody
    public List<Application> AdDifferenceApplicationSearch(@RequestParam("state") String state,@RequestBody Application application){

        //若state=0，则为审核中，，=1为已通过，，=2为未通过
        if (state.equals("0")){
            application.setState("审核中");
        }else if (state.equals("1")){
            application.setState("已通过");
        }else {
            application.setState("未通过");
        }
        application.setDepartment("广东科技学院");
        return applicationService.SearchZpDifferenceApplication(application);
    }
}
